/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entity.Student;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

public class StudentDao extends SimpleJdbcDaoSupport {
    
    public static final String COMMA = ",";
    public static final String SINGLE_QUOTE = "'";
    
    public List<Student> getListStudents() {
        String query = "SELECT * FROM students";
        return getSimpleJdbcTemplate().query(query, new StudentMapper());
    }
    
    public void insetNewStudent(Student s) {
        String query = "INSERT INTO students"
                + " (name,title,supervisor,examiner1,examiner2,president,secretary,additionalmember) "
                + " VALUES(" + SINGLE_QUOTE + s.getName() + SINGLE_QUOTE
                + COMMA + SINGLE_QUOTE + s.getTitle() + SINGLE_QUOTE + COMMA
                + s.getSupervisorId() + COMMA + s.getExaminerId1() + COMMA + s.getExaminerId2()
                + COMMA + s.getPresidentId() + COMMA + s.getSecretaryId() + COMMA
                + s.getAdditionalMemberId() + ")";
        getJdbcTemplate().execute(query);
    }
    
    private class StudentMapper implements ParameterizedRowMapper<Student> {
        
        @Override
        public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
            Student student = new Student();
            student.setName(rs.getString("name"));
            student.setId(rs.getInt("id"));
            student.setSupervisorId(rs.getInt("supervisor"));
            student.setExaminerId1(rs.getInt("examiner1"));
            student.setExaminerId2(rs.getInt("examiner2"));
            student.setPresidentId(rs.getInt("president"));
            student.setSecretaryId(rs.getInt("secretary"));
            student.setAdditionalMemberId(rs.getInt("additionalmember"));
            student.setTitle(rs.getString("title"));
            return student;
        }
    }
}
